package jtech;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;

public class Drawer {
 
	public static final int ALIGN_LEFT   = -1;
	public static final int ALIGN_CENTER =  0;
	public static final int ALIGN_RIGHT  =  1;
	
    public static Font FONT_DEFAULT = new Font(Font.DIALOG, Font.PLAIN, 10);
    
	private static Stroke LINE_WIDTH_1 = new BasicStroke(1);
	private static Stroke LINE_WIDTH_2 = new BasicStroke(2);
	private static Stroke LINE_WIDTH_3 = new BasicStroke(3);
    
    private Graphics2D g2d;
    private boolean fill;
    private int align = -1;
    
    public Graphics2D getG2D() {
        return g2d;
    }

    public void setG2D(Graphics2D g2d) {
        this.g2d = g2d;
    }
    
    public Drawer align(int align) {
    	this.align = align;
    	return this;
    }
    
    public Drawer font(Font font) {
    	g2d.setFont(font);
    	return this;
    }
    
    public Drawer left() {
        align = -1;
        return this;
    }
    
    public Drawer center() {
        align = 0;
        return this;
    }
    
    public Drawer right() {
        align = 1;
        return this;
    }
    
    public void print(float sx, float sy, String str, Object ... args) {
        String str2 = String.format(str, args);
        if (align == 0) {
            sx -= g2d.getFontMetrics().stringWidth(str2) / 2;
        } else if (align == 1) {
            sx -= g2d.getFontMetrics().stringWidth(str2);
        }
        g2d.drawString(str2, Math.round(sx), Math.round(sy));
    }
    
    public Drawer lineWidth1() {
    	g2d.setStroke(LINE_WIDTH_1);
    	return this;
    }
    
    public Drawer lineWidth2() {
    	g2d.setStroke(LINE_WIDTH_2);
    	return this;
    }
    
    public Drawer lineWidth3() {
    	g2d.setStroke(LINE_WIDTH_3);
    	return this;
    }
    
    public Drawer color(int r, int g, int b, int a) {
        g2d.setColor(new Color(r, g, b, a));
        return this;
    }
    
    public Drawer color(Color c) {
        g2d.setColor(c);
        return this;
    }
    
    public Drawer defaultFont() {
        g2d.setFont(FONT_DEFAULT);
        return this;
    }
    
    public Drawer black() {
        g2d.setColor(Color.BLACK);
        return this;
    }
    
    public Drawer white() {
        g2d.setColor(Color.WHITE);
        return this;
    }
    
    public Drawer red() {
        g2d.setColor(Color.RED);
        return this;
    }
    
    public Drawer fill() {
        fill = true;
        return this;
    }
    
    public Drawer fill(Color color) {
        fill = true;
        g2d.setColor(color);
        return this;
    }
    
    public Drawer draw() {
        fill = false;
        return this;
    }
    
    public Drawer draw(Color color) {
        fill = false;
        g2d.setColor(color);
        return this;
    }
    
    public Drawer circle(float sx, float sy, float r) {
        if (fill) {
            g2d.fillOval(Math.round(sx-r), Math.round(sy-r), Math.round(r*2), Math.round(r*2));
        } else {
            g2d.drawOval(Math.round(sx-r), Math.round(sy-r), Math.round(r*2), Math.round(r*2));
        }
        return this;
    }
    
    public Drawer oval(float sx, float sy, float w, float h) {
        if (fill) {
            g2d.fillOval(Math.round(sx), Math.round(sy), Math.round(w), Math.round(h));
        } else {
            g2d.drawOval(Math.round(sx), Math.round(sy), Math.round(w), Math.round(h));
        }
        return this;
    }
    
    public Drawer rect(float x, float y, float w, float h) {
        if (fill) {
            g2d.fillRect(Math.round(x), Math.round(y), Math.round(w), Math.round(h));
        } else {
            g2d.drawRect(Math.round(x), Math.round(y), Math.round(w), Math.round(h));
        }
        return this;
    }
    
    public Drawer square(float x, float y, float w) {
        rect(x, y, w, w);
        return this;
    }
    
    public Drawer line(float x1, float y1, float x2, float y2) {
        g2d.drawLine(Math.round(x1), Math.round(y1), Math.round(x2), Math.round(y2));
        return this;
    }
    
    public Drawer draw(Image img, int dx, int dy, int sx, int sy, int w, int h) {
    	g2d.drawImage(img, dx, dy, dx+w, dy+h, sx, sy, sx+w, sy+h, null);
    	return this;
    }

}
